var goEasy = new GoEasy({
    appkey: 'BC-1a6013607c1045b2abad8def6ba9b11d'
});


function send(headSrc, str) {
    var html = "<div class='send'><div class='msg'><img src=" + headSrc + " />" +
        "<p><i class='msg_input'></i>" + str + "</p></div></div>";
    upView(html);
}

function show(headSrc, str) {
    var html = "<div class='show'><div class='msg'><img src=" + headSrc + " />" +
        "<p><i class='msg_input'></i>" + str + "</p></div></div>";
    upView(html);
}

function upView(html) {
    $('.message').append(html);
    $('body').animate({ scrollTop: $('.message').outerHeight() - window.innerHeight }, 200)
}

function sj() {
    return parseInt(Math.random() * 10)
}
$(function() {
    $('.footer').on('keyup', 'input', function() {
        if ($(this).val().length > 0) { $(this).next().css('background', '#114F8E').prop('disabled', true); } else { $(this).next().css('background', '#ddd').prop('disabled', false); }
    })
    $('.footer p').click(function() {
        var msg = $(this).prev().val();
        goEasy.publish({
            channel: 'demo_channel',
            message: msg
        });		
		$(this).prev().val('');
    });
    goEasy.subscribe({
        channel: 'demo_channel',
        onMessage: function(message) {
        	if(message.content.indexOf('me:')>=0){
        		var m = message.content.replace('me:','');
        		show("images/me.png", m);
        	}else{

        		send("images/she.png", message.content);
        	}
        }
    });
});
